package com.jy.servlet;


import com.jy.dao.UserDao;
import com.jy.service.Orderservice;
import com.jy.service.ShoppingTrolleyService;
import com.jy.service.UserService;
import com.jy.service.impl.OrderSericelmpl;
import com.jy.service.impl.ShoppingTrolleySericelmpl;
import com.jy.service.impl.UserServicelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(UserDao.class);
    private UserService userService;
    private Orderservice orderservice;
    private ShoppingTrolleyService sts;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String register = "";
        String admin = request.getParameter("name");
        String password = request.getParameter("password");
        userService = new UserServicelmpl();
        orderservice = new OrderSericelmpl();
        sts = new ShoppingTrolleySericelmpl();
        if (userService.selelogin(admin,password)==true){
            //判断是从那个页面跳转到登录页面
            register = "加入购物车页面";

            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);

            session.setAttribute("register",register);

            //查询该账号的购物车数量，并给到session里面去
            int Shopping_count = sts.selectByName(admin);
            session.setAttribute("Shopping_count",Shopping_count);
            //登录成功，判断当前用户的订单时候超时，如果超时订单失效
            orderservice.update_order(admin);
            request.getRequestDispatcher("/www.mi.com.jsp").forward(request,response);
            log.debug("登录成功");
        }else if (userService.seleadmin(admin,password)==true){
            HttpSession session = request.getSession();
            session.setAttribute("merchant_admin", admin);
            //登录成功，判断所有的订单时候超时，如果超时订单失效
            orderservice.updateByname_order();
            request.getRequestDispatcher("/merchant").forward(request,response);
            log.debug("登录成功");
        }
        else {
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            log.debug("登录失败");
        }
    }
}
